<!DOCTYPE HTML>
<html>
<head>
    <title>Todo</title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <script src="/scripts/jquery.min.js"></script>
    <link rel="stylesheet" href="/scripts/miniui/themes/default/miniui.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/icons.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/bootstrap/skin.css">
    <link rel="stylesheet" href="/editor/common.css">
    <script src="/scripts/miniui/miniui.js"></script>
</head>
<body  style="padding: 0;width: 100%;height: 100%">
<div class="mini-splitter" style="width:100%;height:100%;">
    <div size="240" showCollapseButton="true">
        <div class="mini-fit">
            <ul id="tree1" class="mini-tree"  url="/getdata/userLevelTree" style="width:100%;"
                showTreeIcon="true" textField="name" idField="id" onnodeclick="onNodeDblClick" parentField="pid" resultAsTree="false"
            >
            </ul>
        </div>
    </div>
    <div id="right" showCollapseButton="true">
        <div id="outImg" >
            <img id="imgId" style="width: 400px" src="" alt="" onclick="smallImg()">
        </div>
        <div style="padding: 5px">
            <span>微信号：</span><input  class="mini-textbox" type="text" id="weChat"/>
            <span>姓名：</span><input  class="mini-textbox" type="text" id="name"/>
            <span>身份证号：</span><input  class="mini-textbox" type="text" id="idCard"/>
            <span>授权代码：</span><input  class="mini-textbox" type="text" id="authCode"/>
            <span>状态：</span><input id="status" class="mini-combobox"  data="userStatus" />
            <a class="mini-button" iconCls="icon-search" onclick="search()">查找</a>
        </div>
        <div style="width:99%;" >
            <div class="mini-toolbar" style="border-bottom:0;padding:0px;">
                <table style="width:100%;">
                    <tr>
                        <td style="width:100%;">
                            <!--<a class="mini-button" iconCls="icon-add" onclick="levelAdd()">增加授权等级</a>-->
                            <a class="mini-button" iconCls="icon-add" onclick="userAdd()">增加</a>
                            <a class="mini-button" iconCls="icon-edit" onclick="userLevelEdit()">编辑</a>
                            <a class="mini-button" iconCls="icon-add" onclick="userLevelAuth()">授权</a>
                            <a class="mini-button" iconCls="icon-remove" onclick="userLevelRemove()">删除</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>

        <div id="datagrid1" class="mini-datagrid" style="width:99%;height: 82%;" multiSelect="true"
             url="/getdata/userLevelManger" idField="id" allowResize="true"
             sizeList="[20,30,50,100]" pageSize="30"
        >
            <div property="columns">
                <div type="checkcolumn" ></div>
                <div field="objectId" width="120" headerAlign="center" allowSort="true" >用户id</div>
                <div field="name" width="80" headerAlign="center" allowSort="true" >用户名</div>
                <div field="authCode" width="80" headerAlign="center" allowSort="true" >授权代码</div>
                <div field="authImage"  width="50" align="center" headerAlign="center" renderer="onIconRenderer" allowSort="true" >授权图像</div>
                <div field="telephone" align="center" align="center" headerAlign="center" width="100" allowSort="true" >电话号</div>
                <div field="weChat" align="left" align="center" headerAlign="center" width="70" allowSort="true" >微信号</div>
                <div field="status" renderer="statusRenderer" align="center" width="50" headerAlign="center" allowSort="true">状态</div>
                <div field="nowTime" renderer="nowTimeRenderer" align="center" width="50" headerAlign="center" allowSort="true">是否有效</div>
                <div field="teamLeader" align="center" width="50" headerAlign="center" allowSort="true" >团队长</div>
                <div field="levelName" align="center" width="50" headerAlign="center" allowSort="true" >授权等级</div>
                <div field="idCard" width="100" align="center" headerAlign="center" allowSort="true" >身份证号</div>
                <div field="startPowerTime" width="100" renderer="onBirthdayRenderer" align="center" headerAlign="center" allowSort="true">授权开始时间</div>
                <div field="endPowerTime" width="100" renderer="onBirthdayRenderer" align="center" headerAlign="center" allowSort="true">授权结束时间</div>
                <div field="createdAt" width="100" renderer="onBirthdayRenderer" align="center" headerAlign="center" allowSort="true">创建日期</div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    var userStatus = [{ id: 1, text: '正常' }, { id: 0, text: '关闭'}, {id:"",text: '全部'}];
    var flag = 1;
    mini.parse();
    var tree = mini.get("tree1");
    var grid = mini.get("datagrid1");

    tree.on("nodeselect", function (e) {
        if (e.isLeaf) {
            grid.load({dept_id: e.node.id});
        } else {
            grid.setData([]);
            grid.setTotalCount(0);
        }
    });
    grid.load();
    grid.on("drawcell", function (e) {
        var  value = e.value;
        column = e.column;
        //将性别文本替换成图片
        if (column.field == "authImage") {
            if(value) {
                e.cellHtml = '<img class="bigImg" onclick="bigImg()" src="http://tribemedia.aladada.com/' + value + '?imageMogr2/thumbnail/640x/format/jpg/interlace/1/auto" width="30" height="30" alt="图片">';
            }else{
                e.cellHtml = '<img class="bigImg" onclick="bigImg()"  src="http://7xiuo5.com1.z0.glb.clouddn.com/fgImage.png?imageMogr2/auto-orient/thumbnail/!100x100r/gravity/Center/crop/100x100/quality/80/auto-orient" width="30" height="30" alt="图片">';
            }
        }
    });
    function onNodeDblClick(e) {
        grid.load({dept_id: e.node.id});
        levelName = e.node.id;
    }
    function search() {
        var weChat = mini.get("weChat").getValue();
        var idCard = mini.get("idCard").getValue();
        var authCode = mini.get("authCode").getValue();
        var status = mini.get("status").getValue();
        var name = mini.get("name").getValue();
        grid.load({
            weChat: weChat,
            idCard:idCard,
            authCode:authCode,
            status:status,
            name:name
        });
    }
    function searchStatus() {
        var status = mini.get("status").getValue();
        grid.load({
            status: status
        });
    }
    $("#name,#objectId,#tel").bind("keydown", function (e) {
        if (e.keyCode == 13) {
            search();
        }
    });


    function userLevelEdit() {
        var row = grid.getSelected();
        if (row) {
            mini.open({
                url: "/getdata/userLevelEdit?id="+row.objectId,
                title: "编辑用户信息", width: 600, height: 490,

                ondestroy: function (action) {

                    grid.reload();

                }
            });

        } else {
            alert("请选中一条记录");
        }

    }
    function userAdd() {
        mini.open({
            url: "/getdata/userAdd",
            title: "编辑用户信息", width: 600, height: 490,

            ondestroy: function (action) {
                grid.reload();
            }
        });
    }
    function levelAdd() {
        mini.open({
            url: "/getdata/addLevelTree",
            title: "编辑用户信息", width: 600, height: 300,

            ondestroy: function (action) {

                grid.reload();

            }
        });
    }
    function userLevelRemove() {
        var row = grid.getSelected();
        if (row) {
            $.ajax({
                url:"/getdata/editUserInformation",
                type:'POST',
                data:{
                    objectId:row.objectId,
                    status:0
                },
                success:function (result) {
                    grid.reload();
                }
            });
        } else {
            alert("请选中一条记录");
        }

    }
    function userLevelAuth() {
        var rows = grid.getSelecteds();
        if (rows) {
            var ids = [];
            for (var i = 0, l = rows.length; i < l; i++) {
                var r = rows[i];
                var objectId = r.objectId.replace("\"","");
                ids.push(objectId);
            }
            console.log(ids);
            var id = ids.join('-');
            grid.loading("操作中，请稍后......");
            $.ajax({
                url:"/getdata/userLevelAuth",
                type:'POST',
                data:{
                    objectId:id,
                },
                success:function (result) {
                    if (result=='10002'){
                        alert('证书生成失败，服务器异常');
                    }else if(result=='10000'){
                        alert("修改成功");
                        grid.reload();
                    }
                }
            });
        } else {
            alert("请选中一条记录");
        }

    }

    function bigImg() {
        var row = grid.getSelected();
        if (row) {
            if(flag==1) {
                var path = "http://tribemedia.aladada.com/"+row.authImage;
                console.log(path);
                $("#imgId").attr('src', path);
                $("#outImg").css('display','block');
            }
        }
    }
    function smallImg() {
        $("#outImg").css('display','none');
    }


    function onBirthdayRenderer(e) {
        var value = e.value;
        if (value) return mini.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
        return "";
    }
    function statusRenderer(e) {
        var value = e.value;
        if (value==1) {
            return (value, '正常')
        }else if(value==0){
            return (value, '关闭')
        }
    };
    function nowTimeRenderer(e) {
        var value = e.value;
        if (value==1) {
            return (value, '有效')
        }else if(value==0){
            e.cellStyle = "color:red";
            return (value, '失效')
        }
    };
</script>
</html>